package com.study.userserviceprovider.biz;

import com.study.api.Result;
import com.study.exception.BizException;
import com.study.userserviceprovider.dto.AuthLoginDto;
import com.study.userserviceprovider.mapper.entity.TbMember;
import com.study.userserviceprovider.util.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @description：
 * @author：李军荣
 * @date: 2020/12/28 11:02
 */
@Slf4j
public abstract class AbstarctLogin implements Login{
    public static Map<Integer,AbstarctLogin> loginMap = new ConcurrentHashMap<>();

    @PostConstruct
    public void init(){
        loginMap.put(getLoginType(),this);
    }


    @Override
    public Result doLogin(AuthLoginDto authLoginDto) throws BizException {
        log.info("begin AbstarctLogin doLogin"+authLoginDto);
        validate(authLoginDto);
        TbMember tbMember = doProcessor(authLoginDto);
        Map<String,Object> payload = new HashMap<>();
        payload.put("uid",tbMember.getId());
        payload.put("exp", DateTime.now().plusHours(1).toDate().getTime()/1000);
        String token = JwtUtil.generatorToken(payload);
        return new Result.Builder().data(token).ok().bulider();
    }

    /**
     * @Title: getLoginType
     * @Description: 获取子类登录类型
     * @Date 2020/12/28 11:04
     * @author 李军荣
     * @Param:
     * @return int
     */
    protected abstract int getLoginType();


    /**
     * @Title: validate
     * @Description: 登录校验
     * @Date 2020/12/28 11:05
     * @author 李军荣
     * @Param: authLoginDto
     * @return boolean
     */
    protected abstract boolean validate(AuthLoginDto authLoginDto);


    /**
     * @Title: doProcessor
     * @Description: 登录处理
     * @Date 2020/12/28 11:05
     * @author 李军荣
     * @Param: authLoginDto
     * @return boolean
     */
    protected abstract TbMember doProcessor(AuthLoginDto authLoginDto);
}
